package us.icitap.timsportal.management;

import java.util.Date;

import us.icitap.timsportal.business.DomainUser;
import us.icitap.timsportal.entity.User;
import us.icitap.timsportal.resources.TimsPortalLabels;
import us.icitap.timsportal.util.SearchListener;
import us.icitap.timsportal.web.TimsPortalApp;
import us.icitap.timsportal.web.custom.LinkPortalButton;
import us.icitap.timsportal.web.custom.PNativeButton;
import us.icitap.timsportal.web.custom.PTextField;
import us.icitap.timsportal.web.custom.PortalLabel;
import us.icitap.timsportal.web.custom.PortalVerticalLayout;

import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;

@SuppressWarnings("serial")
public class UserSearch extends PortalVerticalLayout implements Button.ClickListener{
	
	final PTextField usernameTextField = new PTextField();
	final PNativeButton searchBtn = new PNativeButton(TimsPortalLabels.getLabel("btn.search"));
	final LinkPortalButton resetLink = new LinkPortalButton(TimsPortalLabels.getLabel("btn.reset"));
	final LinkPortalButton domainSearchBtn = new LinkPortalButton(TimsPortalLabels.getLabel("btn.domainSearchBtn"));
	
	private DomainSearchWindow domainWindow;
	private TimsPortalApp app = null;
	private Label notFoundUserMsg;

	public UserSearch() {
		setMargin(true);
		setWidth("550px");
		setSpacing(true);
		setStyleName("timsportal");
		
		PortalLabel title = new PortalLabel(TimsPortalLabels.getLabel("title.userSearchView"), Label.CONTENT_XHTML);
		title.setSizeFull();
		title.setStyleName("menu");
		
		notFoundUserMsg = new Label("<font color=\"red\">Ky perdorues nuk ndodhet ne sistem !</font>", Label.CONTENT_XHTML);
		notFoundUserMsg.setSizeFull();
		notFoundUserMsg.setVisible(false);

		domainSearchBtn.setImmediate(true);
		usernameTextField.setRequired(true);
		usernameTextField.setWidth("200px");
//		usernameTextField.setInputPrompt(TimsPortalLabels.getLabel("label.username"));
		
		HorizontalLayout hl = new HorizontalLayout();
		hl.setSpacing(true);
		hl.addComponent(usernameTextField);
		hl.setComponentAlignment(usernameTextField, Alignment.MIDDLE_CENTER);
		hl.addComponent(searchBtn);		
		hl.addComponent(resetLink);
		hl.setComponentAlignment(resetLink, Alignment.BOTTOM_CENTER);
		
		addComponent(title);
		addComponent(hl);
		addComponent(notFoundUserMsg);
		addComponent(domainSearchBtn);
		
		domainWindow = new DomainSearchWindow();
		domainWindow.addSearchListener(new SearchListener(){
			@Override
			public void searchFinished(Object o) {
				DomainUser dUser = (DomainUser) o;
				User user = app.getPortalBean().getUserEntity(dUser.getAccountName().toUpperCase());
				if(user==null){
					user = new User();
					user.setUsername(dUser.getAccountName());
					user.setEmail(dUser.getEmail());
					user.setStartDate(new Date());
					user.setRecordDate(new Date());
					user.setRecordUser(app.getSessionHandler().getUsername());
				}
				app.setMainComponent(app.getUserEditor());
				app.getUserEditor().setDataSource(user);
			}
			
		});
		
	}

	@Override
	public void buttonClick(ClickEvent event) {
		final Button button = event.getButton();

		if (button == searchBtn) {
			
			if(!usernameTextField.isValid())return;			
			String searchTerm = (String) usernameTextField.getValue();
			User user = app.getPortalBean().getUserEntity(searchTerm);
			if(user!=null){
				app.setMainComponent(app.getUserEditor());
				app.getUserEditor().setDataSource(user);
			}
			notFoundUserMsg.setVisible(user==null);
		} else if (button == resetLink) {
			clear();
		} else if(button== domainSearchBtn){
			if(domainWindow.getParent()==null){
				getWindow().addWindow(domainWindow);
				domainWindow.clear();
			}
			
		}

	}
	
	public void clear(){
		notFoundUserMsg.setVisible(false);
		domainSearchBtn.setVisible(true);
		usernameTextField.setValue("");
		usernameTextField.focus();
	}
	
	@Override
	public void attach(){
		super.attach();
		
		app = getApplication();
		
		searchBtn.addListener(this);
		resetLink.addListener(this);
		domainSearchBtn.addListener((ClickListener)this);
			
	}

}
